package com.choosefine.springboot.devops.base.config;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2017/2/14 0014.
 */
public class FileUtil {
    public static boolean uploadsUtil(String filePath,MultipartFile[] files,List<String> urls) {
        boolean result=false;
        for (MultipartFile now : files) {
            MultipartFile file = now;
            if (file.isEmpty()) {
                return false;
            }
            // 获取文件名
            String fileName = System.currentTimeMillis()+file.getOriginalFilename();
//            logger.info("上传的文件名为：" + fileName);
            // 获取文件的后缀名
            String suffixName =  System.currentTimeMillis()+fileName.substring(fileName.lastIndexOf("."));
//            logger.info("上传的后缀名为：" + suffixName);
            // 文件上传路径

            // 解决中文问题，liunx下中文路径，图片显示问题
            // fileName = UUID.randomUUID() + suffixName;
            File dest = new File(filePath + fileName);
            // 检测是否存在目录
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            try {
                file.transferTo(dest);
                urls.add(suffixName);
                result=true;
            } catch (IllegalStateException e) {
                e.printStackTrace();
                return false;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
        return result;
    }

    public static boolean uploadsUtil(String filePath,MultipartFile file,List<String> urls) {
        boolean result=false;
            if (file.isEmpty()) {
                return false;
            }
            // 获取文件名
            String fileName = System.currentTimeMillis()+file.getOriginalFilename();
//            logger.info("上传的文件名为：" + fileName);
            // 获取文件的后缀名
            String suffixName =  System.currentTimeMillis()+fileName.substring(fileName.lastIndexOf("."));
//            logger.info("上传的后缀名为：" + suffixName);
            // 文件上传路径

            // 解决中文问题，liunx下中文路径，图片显示问题
            // fileName = UUID.randomUUID() + suffixName;
            File dest = new File(filePath + fileName);
            // 检测是否存在目录
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            try {
                file.transferTo(dest);
                urls.add(suffixName);
                result=true;
            } catch (IllegalStateException e) {
                e.printStackTrace();
                return false;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        return result;
    }


    public static boolean deleteUtil(String filePath,List<String> urls) {
        boolean result=false;
        if(urls==null||urls.size()==0){
            return true;
        }
        for (String now : urls) {
            File file = new File(filePath + now);
            if (file.isFile() && file.exists()) {
                file.delete();
                result = true;
            }else{
                return false;
            }
        }
        return result;
    }

}
